﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.ISerivces;
using DWF.Studio.Services;
using DWF.Studio.Share.Log;
using DWF.Studio.Share.Utils;
using Ookii.Dialogs.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ThoughtWorks.QRCode.Codec;
using WpfMessageBoxLibrary;

namespace DWF.Studio.Views
{
    /// <summary>
    /// WindowLicenseNotice.xaml 的交互逻辑
    /// </summary>
    public partial class WindowLicense
    {
        private IEnvironmentSerivce _environmentSerivce = null;
        private IAppService _appService = null;

        [DllImport("gdi32.dll", SetLastError = true)]
        private static extern bool DeleteObject(IntPtr hObject);

        public WindowLicense(bool isPass = true)
        {
            InitializeComponent();
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                init(isPass);
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void init(bool isPass)
        {
            this._environmentSerivce = new EnvironmentSerivce();
            this._appService = AppService.Instance;
            this.tbProductName.Text = AssemblyInfoHelper.AssemblyProduct;
            this.tbkProductName.Text = AssemblyInfoHelper.AssemblyProduct;
            this.tbkProductVersion.Text = new Version(AssemblyInfoHelper.AssemblyVersion).ToString(3);

            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeVersion = 0;
            Bitmap img = qrCodeEncoder.Encode(this.tbkProductName.Text, Encoding.UTF8);//指定utf-8编码， 支持中文 
            
            IntPtr hBitmap = img.GetHbitmap();
            ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            if (!DeleteObject(hBitmap))//记得要进行内存释放。否则会有内存不足的报错。
            {
                throw new System.ComponentModel.Win32Exception();
            }
            this.tbkMachineIDImage.Source = wpfBitmap;

            this.tbkCopyright.Text = AssemblyInfoHelper.AssemblyCopyright;


            var osinof = _environmentSerivce.GetCurrentOSInfo();
            this.tbkOS.Text = osinof.Caption;
        }

        private void btnTrial_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        
        private void BtnCopyId_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"产品名称：{this.tbkProductName.Text}{Environment.NewLine}");
            sb.Append($"联系我们：processgo@inossem.com{Environment.NewLine}");
            sb.Append($"官方网站：http://www.processgo.cn");
            Clipboard.SetDataObject(sb.ToString());
            
            var msgProperties = new WpfMessageBoxProperties()
            {
                Button = MessageBoxButton.OK,
                ButtonOkText = LocalizationProvider.GetLocalizedString("MessageBox_Button_OK"),
                Image = MessageBoxImage.Information,
                Text = LocalizationProvider.GetLocalizedString("WinLicense_MsgBox_Msg_CopyID"),
                Title = LocalizationProvider.GetLocalizedString("UCtrlProjectExplorer_MsgBox_Caption_Publish"),
            };

            Window owner = Window.GetWindow(this);
            MessageBoxResult dlgResult = WpfMessageBox.Show(owner, ref msgProperties);
        }

        private void Email_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("mailto:processgo@inossem.com");
        }

        private void Link_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.inossem.com");
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
